#include "datafile.h"
#include <QDebug>
DataFile::DataFile(QObject *parent) :
	QObject(parent),
	m_vector(new QVector<BookmarkItem*>)
{
}

QVector<BookmarkItem *> *DataFile::read(QString fileName)
{
	QFile file(fileName);
	file.open(QIODevice::ReadOnly);
	QString buf, text, url;
	while(!file.atEnd()) {
		buf = file.readLine();
		if(buf.at(0) == '<') {
			text = buf.mid(1, buf.length() - 2);
			BookmarkItem *folder = new BookmarkItem("", text, QIcon(), true);
			buf = file.readLine();
			while(buf.at(0) != '>') {
				url = buf.mid(0, buf.length() - 1);
				text = toNormalText(buf.mid(buf.lastIndexOf("/") + 1));
				folder->append(new BookmarkItem(url, text, QIcon(), false, folder));
				buf = file.readLine();
			}
			m_vector->append(folder);
		}
	}
	return m_vector;
}
inline QString DataFile::toNormalText(QString badText) {
	badText.replace(QRegExp("[_]"), " ");
	badText = badText.left(1).toUpper() + badText.mid(1);
	badText.remove('\n');
	return badText;
}
